package com.situ.mall.service.impl;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.mall.common.ServerResponse;
import com.situ.mall.dao.UserMapper;
import com.situ.mall.entity.User;
import com.situ.mall.service.IUserService;
import com.situ.mall.util.MyBatisUtil;

@Service
public class UserServiceImpl implements IUserService {

	@Autowired
	private UserMapper userMapper;
	
	@Override
	public User selectByName(String username) {
		User user = userMapper.selectByName(username);
		return user;
	}

	@Override
	public User selectByEmail(String email) {
		User user = userMapper.selectByEmail(email);
		return user;
	}

	@Override
	public boolean register(User user) {
		int count = userMapper.insert(user);
		return count == 1 ? true : false;
	}

	@Override
	public ServerResponse login(String username, String password, HttpSession session) {
		User user = userMapper.login(username,password);
		if (user != null) {
			session.setAttribute("user", user);
			return ServerResponse.createSuccess("登陆成功");
		}
		return ServerResponse.createError("登陆失败");
	}

}
